﻿@page "/user_auth/resource"
<div>
    <PageContainer Title="">
        <Breadcrumb>
            <Breadcrumb>
                <BreadcrumbItem>用户权限</BreadcrumbItem>
                <BreadcrumbItem>资源管理</BreadcrumbItem>
            </Breadcrumb>
        </Breadcrumb>
        <ChildContent>
            <div>
                <Row>
                    <AntDesign.Col Span="8">
                        <Row>
                            <AntDesign.Col Span="18">
                                <Space>
                                    <ResourceAuthorize ResourceKey="user_auth_resource_switch">
                                        <SpaceItem>
                                            <Tooltip Placement="PlacementType.TopLeft" Title="@(isExpanded?"收起":"展开")">
                                                <Button Type="@ButtonType.Primary" Icon="@(isExpanded?"up":"down")" Size="@ButtonSize.Small" OnClick="OnExpandClick" />
                                            </Tooltip>
                                        </SpaceItem>
                                    </ResourceAuthorize>
                                    <ResourceAuthorize ResourceKey="user_auth_resource_refresh">
                                        <SpaceItem>
                                            <Tooltip Placement="PlacementType.TopLeft" Title="@("刷新")">
                                                <Button Type="@ButtonType.Primary" Icon="reload" Size="@ButtonSize.Small" OnClick="OnReloadClick" />
                                            </Tooltip>
                                        </SpaceItem>
                                    </ResourceAuthorize>
                                    <ResourceAuthorize ResourceKey="user_auth_resource_add">
                                        <SpaceItem>
                                            <Tooltip Placement="PlacementType.TopLeft" Title="@("添加子节点")">
                                                <Button Type="@ButtonType.Primary" Icon="plus-circle" Size="@ButtonSize.Small" OnClick="OnAddChildNodeClick" />
                                            </Tooltip>
                                        </SpaceItem>
                                    </ResourceAuthorize>
                                    <ResourceAuthorize ResourceKey="user_auth_resource_edit">
                                        <SpaceItem>
                                            <Tooltip Placement="PlacementType.TopLeft" Title="@("编辑选中节点")">
                                                <Button Type="@ButtonType.Primary" Icon="edit" Size="@ButtonSize.Small" OnClick="OnEditSelectedNodeClick" />
                                            </Tooltip>
                                        </SpaceItem>
                                    </ResourceAuthorize>
                                    <ResourceAuthorize ResourceKey="user_auth_resource_delete">
                                        <SpaceItem>
                                            <Tooltip Placement="PlacementType.TopLeft" Title="@("删除选中及所有子节点")">
                                                <Button Type="@ButtonType.Primary" Icon="delete" Danger Size="@ButtonSize.Small" OnClick="OnDeleteSelectedNodeClick" />
                                            </Tooltip>
                                        </SpaceItem>
                                    </ResourceAuthorize>
                                </Space>
                            </AntDesign.Col>
                        </Row>
                        <Row>
                            <AntDesign.Col Span="24" Style="margin-top:10px">
                                <Spin Spinning="@treeIsLoading">
                                    <Tree @ref="tree"
                                          ShowLine
                                          ShowIcon
                                          TitleExpression="x=> ((ResourceDto)x.DataItem).GetFullName()"
                                          ChildrenExpression="x=>((ResourceDto)x.DataItem).Children"
                                          IconExpression="x=>((ResourceDto)x.DataItem).Icon"
                                          IsLeafExpression="x=>false"
                                          OnClick="OnNodeClick"
                                          OnExpandChanged="e=> { isExpanded = e.Node.IsExpanded; }"
                                          Style="min-height:466px;">
                                    </Tree>
                                </Spin>
                            </AntDesign.Col>
                        </Row>
                    </AntDesign.Col>
                    <AntDesign.Col Span="12" Offset="2">
                        <Spin Spinning="descriptionsIsLoading">
                            <Descriptions Title="资源信息" Bordered Layout="@DescriptionsLayout.Vertical" Style="margin-top:10px">
                                <DescriptionsItem Title="标示">@editModel.Id</DescriptionsItem>
                                <DescriptionsItem Title="名称">@editModel.Name</DescriptionsItem>
                                <DescriptionsItem Title="键值">@editModel.Key</DescriptionsItem>
                                <DescriptionsItem Title="类型">
                                    @(EnumExtension.GetEnumDescription(editModel.Type))
                                </DescriptionsItem>
                                <DescriptionsItem Title="排序">
                                    @editModel.Order
                                </DescriptionsItem>
                                <DescriptionsItem Title="图标">
                                    @if (!string.IsNullOrEmpty(editModel.Icon))
                                    {
                                        <Icon Type="@editModel.Icon"></Icon>
                                    }
                                    <sppn>@editModel.Icon</sppn>
                                </DescriptionsItem>
                                <DescriptionsItem Title="锁定">
                                    @(editModel.IsLocked?"是":"否")
                                </DescriptionsItem>
                                <DescriptionsItem Title="创建时间">
                                    @editModel.CreatedTime.ToString("yyyy-MM-dd HH:mm:ss")
                                </DescriptionsItem>
                                <DescriptionsItem Title="地址">
                                    <Space>
                                        @if (editModel.Type.Equals(ResourceType.API) && editModel.Method.HasValue)
                                        {

                                            <SpaceItem>@(EnumExtension.GetEnumDescription(editModel.Method))</SpaceItem>
                                        }
                                        <SpaceItem>@editModel.Path</SpaceItem>
                                    </Space>
                                </DescriptionsItem>
                                <DescriptionsItem Title="备注" Span="3">
                                    @editModel.Remark
                                </DescriptionsItem>
                            </Descriptions>
                        </Spin>
                    </AntDesign.Col>
                </Row>
            </div>
        </ChildContent>
    </PageContainer>
</div>

<Drawer Closable="true" Width="460" Visible="drawerVisible" Placement="left" Title="@drawerTitle" OnClose="_=>OnDrawerClose()">
    <Form Loading="@formIsLoading"
          Model="@editModel"
          LabelCol="new ColLayoutParam { Span = 8 }"
          WrapperCol="new ColLayoutParam { Span = 16 }"
          OnFinish="OnFormFinish"
          OnFinishFailed="OnFormFinishFailed"
          Context="model"
          Style="min-height:600px;">
        <FormItem Label="标示">
            <Input @bind-Value="@model.Id" Disabled />
        </FormItem>
        <FormItem Label="名称">
            <Input @bind-Value="@model.Name" />
        </FormItem>
        <FormItem Label="键值">
            <Input @bind-Value="@model.Key" />
        </FormItem>
        <FormItem Label="类型">
            <RadioGroup @bind-Value="@(model.Type)">

                @if (model.Id !=Guid.Empty && model.Type.Equals(ResourceType.ROOT))
                {
                    <Radio Value="@(ResourceType.ROOT)">@EnumExtension.GetEnumDescription(ResourceType.ROOT)</Radio>
                }
                else
                {
                    foreach (var gitem in EnumExtension.EnumToDictionary<ResourceType>())
                    {
                        if ((int)gitem.Key >= (int)currentEditResourceType)
                        {
                            <Radio Value="@(gitem.Key)">@gitem.Value</Radio>
                        }
                    }
                }
            </RadioGroup>
        </FormItem>
        @if (editModel.Type.Equals(ResourceType.API))
        {
            <FormItem Label="请求方法">
                <SimpleSelect 
                              @bind-Value="@editModelHttpMethodType"
                              DefaultValue="@((model.Method ?? HttpMethodType.GET).ToString())" 
                              Style="width:120px;" 
                              >
                    <SelectOptions>
                        @foreach (var gitem in EnumExtension.EnumToDictionary<HttpMethodType>())
                        {
                            <SimpleSelectOption Value="@(gitem.Key.ToString())" Label="@(gitem.Value)"></SimpleSelectOption>
                        }
                    </SelectOptions>
                </SimpleSelect>
            </FormItem>
        }
        <FormItem Label="地址">
            <Input @bind-Value="@model.Path" />
        </FormItem>
        <FormItem Label="图标">
            <Input @bind-Value="@model.Icon" />
        </FormItem>
        <FormItem Label="排序">
            <AntDesign.InputNumber @bind-Value="@model.Order" />
        </FormItem>
        <FormItem Label="是否锁定">
            <Switch @bind-Value="@model.IsLocked"></Switch>
        </FormItem>
        <FormItem Label="备注">
            <TextArea AutoSize="true" MinRows="2" MaxRows="6" @bind-Value="@model.Remark"></TextArea>
        </FormItem>
        <FormItem WrapperCol="new ColLayoutParam { Offset = 4, Span = 20 }">
            <Button Type="@ButtonType.Primary" HtmlType="submit">
                保存
            </Button>
            <Button OnClick="_=>OnDrawerClose()">
                取消
            </Button>
        </FormItem>

    </Form>
</Drawer>
